﻿using BuscadorDePreco.DataContract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace BuscadorDePreco.Data
{
    public class EmpresaTableAdapter
    {

        /// <summary>
        /// Adiciona uma nova empresa.
        /// </summary>
        public int AdicionarEmpresa(EmpresaData empresa)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            int id = -1;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    Empresa emp = EmpresaData.ToEmpresaObj(empresa);

                    buscadorEntities.Empresa.AddObject(emp);
                    buscadorEntities.SaveChanges();

                    id = emp.Id;
                }
                scope.Complete();
            }

            return id;
        }

        public List<EmpresaData> RecuperaEmpresas()
        {
            List<EmpresaData> empList = new List<EmpresaData>();

            using (BuscadorEntities buscadorEntities = new BuscadorEntities())
            {
                var empresas = (from emp in buscadorEntities.Empresa
                                select emp).ToList();

                foreach (Empresa empresa in empresas)
                    empList.Add(EmpresaData.ToEmpresaData(empresa));
            }

            return empList;
        }

        public void ExcluirEmpresa(int id)
        {
            TransactionOptions transactionConfig = new TransactionOptions();
            transactionConfig.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionConfig))
            {
                using (BuscadorEntities buscadorEntities = new BuscadorEntities())
                {
                    var empresa = (from emp in buscadorEntities.Empresa
                                    where emp.Id == id
                                    select emp).ToList();

                    if (empresa != null && empresa.Count > 0)
                    {
                        buscadorEntities.Empresa.DeleteObject(empresa.First());

                        var precos = (from prec in buscadorEntities.Preco
                                      where prec.EmpresaId == id
                                      select prec).ToList();

                        if (precos != null && precos.Count > 0)
                        {
                            foreach(Preco p in precos)
                                buscadorEntities.Preco.DeleteObject(p);
                        }

                        buscadorEntities.SaveChanges();
                    }
                }
                scope.Complete();
            }
        }
    }
}